/*
 * Created on 29 Mar 2007 by Andrew White
 */

package com.volatileshooter.networking.server.util;

import java.io.IOException;
import java.text.DateFormat;
import java.util.Date;
import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;

/*
 * A logger class that handles output to the command line, as well as writing
 * this, and more detailed error messages to a log file.
 */
public class Log {
    
    // log to file
    private static Logger log;
    
    /**
     * Creates new instance of log, this will log only to the console initially
     * until setLogFile() is called. Exception stack traces are only logged
     * to a log file.
     * 
     * @param name Name of logger
     */
    public Log(String name) {
       // start new logger
       log = Logger.getLogger(name);
       // prevent logging to the command line
       log.setUseParentHandlers(false);
    }
    
    /**
     * Sets the path of the log file. Rotating log files are used, keeping up to 3
     * log files. Each log file has a limit of 1mb before the logger starts logging
     * to the next log file.
     * 
     * @param path Path of log files
     * @throws SecurityException
     * @throws IOException
     */
    public static void setLogFile(String path) throws SecurityException, IOException {
        // set 1mb limit, cycle 3 log files, and append
        FileHandler fh = new FileHandler(path, 1000000, 3, true);
        // log using simple text output rather than xml
        fh.setFormatter(new SimpleFormatter());
        // use this handler on the log
        log.addHandler(fh);
        // log info
        info("Starting log");
    }
    
    /**
     * Log info to the console on the sys.out and to the log file.
     * 
     * @param msg Message to log
     */
    public static void info(String msg) {
        // output to log file
        log.info(msg);
        
        // output to console
        System.out.println(msg);
    }
    
    /**
     * Log warning to the console on the std.err and to the log file. Exceptions
     * are only logged to the log file. If no message is given then the message of
     * the exception is used.
     * 
     * @param msg Message to log
     * @param e Exception to log
     */
    public static void warning(String msg, Exception e) {
        // use the exception message if no message is given
        if ((msg == "") && (e != null)) {
            msg = e.getMessage();
        }
        // output to log file
        log.log(Level.WARNING, msg, e);
        
        // output to console
        System.err.println("WARNING: " + msg);
    }
    
    /**
     * Log error to the console on the std.err and to the log file. Exceptions
     * are only logged to the log file. If no message is given then the message of
     * the exception is used.
     * 
     * @param msg Message to log
     * @param e Exception to log
     */
    public static void error(String msg, Exception e) {
        // use the exception message if no message is given
        if ((msg == "") && (e != null)) {
            msg = e.getMessage();
        }
        // output to log file
        log.log(Level.SEVERE, msg, e);
        
        // output to console
        System.err.println("ERROR: " + msg);
    }
    
    /**
     * Gets a string representing the current date.
     * 
     * @return Date string
     */
    public static String getDateStr() {
        return DateFormat.getDateTimeInstance().format(new Date());
    }

}
